package com.songlanyun.modules.group.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.group.entity.ProductGoodsGroupEntity;
import com.songlanyun.modules.group.service.ProductGoodsGroupService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 商家店铺商品分组表（只用来在商家店铺内展示）
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-03 12:57:25
 */
@RestController
@RequestMapping("admin/goods_group")
public class ProductGoodsGroupAdminController extends AbstractController {
    @Autowired
    private ProductGoodsGroupService productGoodsGroupService;

    /**
     * 所有分组
     */
    @RequestMapping("/all")
    //@RequiresPermissions("shop:goods_group:all")
    public R all(){
        List<Map<String, Object>> objs = productGoodsGroupService.getAllGoodsGroup(this.getShopId());
        return R.ok().put("list", objs);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("shop:goods_group:info")
    public R infoByShopId(@PathVariable("id") Long id){
        ProductGoodsGroupEntity productGoodsGroup = productGoodsGroupService.getByShopIdAndId(this.getShopId(),id);
        return R.ok().put("data", productGoodsGroup);
    }


    /**
     * 保存
     */
    @SysLog("保存商家店铺商品分组")
    @PostMapping("/create")
    //@RequiresPermissions("group:goods_group:create")
    public R create(@RequestBody ProductGoodsGroupEntity entity){

        ProductGoodsGroupEntity obj =productGoodsGroupService.add(this.getShopId(),entity);
        return R.ok().put("data", obj);
    }

    /**
     * 修改
     */
    @SysLog("修改商家店铺商品分组")
    @PutMapping("/update")
    //@RequiresPermissions("group:productgoodsgroup:update")
    public R update(@RequestBody ProductGoodsGroupEntity entity){
        ProductGoodsGroupEntity obj = productGoodsGroupService.update(this.getShopId(),entity);
        return R.ok().put("data", obj);
    }

    /**
     * 删除
     */
    @SysLog("删除商家店铺商品分组")
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions("group:productgoodsgroup:update")
    public R delete(@PathVariable("id") Long id){
        productGoodsGroupService.deleteByShopIdAndId(this.getShopId(),id);
        return R.ok();
    }

    /**
     * 第一级商品分组
     */
    @RequestMapping("/first")
    //@RequiresPermissions("system:category:all")
    public R first(){
        List<ProductGoodsGroupEntity> objs = productGoodsGroupService.getFirstProductGoods(this.getShopId());
        return R.ok().put("list", objs);
    }

}
